from rest_framework import serializers
from .models import User

class UserSerializer(serializers.ModelSerializer):
    phone = serializers.CharField(max_length=11, required=True)
    password = serializers.CharField(max_length=32, required=True)
    nickname = serializers.CharField(required=True)
    gender = serializers.IntegerField(required=True)
    job_title = serializers.CharField(required=False)
    introduction = serializers.CharField(required=False)
    avatar = serializers.CharField(required=False)

    def validate_phone(self, value):
        if User.objects.filter(phone=value).exists():
            raise serializers.ValidationError("手机号已存在")
        return value

    class Meta:
        model = User
        fields = '__all__'

# 创建获取用户详细信息的序列化器
class UserDetailSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        exclude = ['_password','create_at', 'update_at']